package net.algid.purchase.gui.records;

import java.util.List;

import net.algid.purchase.gui.records.SupplierListDialog.SupplierContentProvider;
import net.algid.purchase.valueObject.Good;
import net.algid.purchase.valueObject.GoodData;
import net.algid.purchase.valueObject.Supplier;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.viewers.ComboViewer;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class GoodDialog extends Dialog implements IConstants {
	private Text nameTxt;
	private Text normaticleTxt;
	private ComboViewer supplierCv;
	final private GoodData good2Edit;
	final private List<Supplier> supplierList;

	/**
	 * Create the dialog.
	 * 
	 * @param parentShell
	 */
	public GoodDialog(Shell parentShell, GoodData good2Edit,
			List<Supplier> supplierList) {
		super(parentShell);
		setShellStyle(SWT.DIALOG_TRIM);
		this.good2Edit = good2Edit;
		this.supplierList = supplierList;
	}

	/**
	 * Create contents of the dialog.
	 * 
	 * @param parent
	 */
	@Override
	protected Control createDialogArea(Composite parent) {
		Composite container = (Composite) super.createDialogArea(parent);
		GridLayout gridLayout = (GridLayout) container.getLayout();
		gridLayout.verticalSpacing = 5;
		gridLayout.marginWidth = 5;
		gridLayout.horizontalSpacing = 5;
		gridLayout.marginHeight = 5;
		gridLayout.numColumns = 2;

		Label nameLbl = new Label(container, SWT.NONE);
		nameLbl.setText(Messages.getString("Good.name.text")); //$NON-NLS-1$

		nameTxt = new Text(container, SWT.BORDER);
		nameTxt.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent arg0) {
				updateOkBtnState();
			}
		});
		nameTxt.setBackground(OBLIGATORY_FIELD_COLOR);
		nameTxt.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1));
		nameTxt.setTextLimit(100);

		Label normaticleLbl = new Label(container, SWT.NONE);
		normaticleLbl.setText(Messages.getString("Good.normaticle.text")); //$NON-NLS-1$

		normaticleTxt = new Text(container, SWT.BORDER);
		normaticleTxt.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent arg0) {
				updateOkBtnState();
			}
		});
		normaticleTxt.setBackground(OBLIGATORY_FIELD_COLOR);
		normaticleTxt.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true,
				false, 1, 1));
		normaticleTxt.setTextLimit(50);

		Label supplierLbl = new Label(container, SWT.NONE);
		supplierLbl.setText(Messages.getString("Good.supplier.text")); //$NON-NLS-1$

		supplierCv = new ComboViewer(container, SWT.READ_ONLY);
		supplierCv.addSelectionChangedListener(new ISelectionChangedListener() {
			public void selectionChanged(SelectionChangedEvent arg0) {
				updateOkBtnState();
			}
		});
		supplierCv
				.setLabelProvider(new SupplierListDialog.SupplierLabelProvider());
		supplierCv.setContentProvider(new SupplierContentProvider());

		final Combo combo = supplierCv.getCombo();
		combo.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1,
				1));
		combo.setBackground(OBLIGATORY_FIELD_COLOR);

		initData();

		return container;
	}

	private void initData() {
		if (supplierList != null) {
			supplierCv.setInput(supplierList.toArray(new Supplier[supplierList
					.size()]));
		} else {
			supplierCv.setInput(new Supplier[0]);
		}
		if (good2Edit != null && good2Edit.good != null) {
			nameTxt.setText(good2Edit.good.name != null ? good2Edit.good.name
					: "");
			normaticleTxt
					.setText(good2Edit.good.nomarticle != null ? good2Edit.good.nomarticle
							: "");
			supplierCv
					.setSelection(new StructuredSelection(good2Edit.supplier));
		} else {
			nameTxt.setText("");
			normaticleTxt.setText("");
		}

	}

	@Override
	protected void buttonPressed(int buttonId) {
		if (buttonId == IDialogConstants.OK_ID && good2Edit != null) {
			if (good2Edit.good == null) {
				good2Edit.good = new Good();
			}
			good2Edit.good.name = nameTxt.getText();
			good2Edit.good.nomarticle = normaticleTxt.getText();
			good2Edit.supplier = (Supplier) ((IStructuredSelection) supplierCv
					.getSelection()).getFirstElement();
			good2Edit.good.supplierRFCode = good2Edit.supplier.code;
		}
		super.buttonPressed(buttonId);
	}

	/**
	 * Create contents of the button bar.
	 * 
	 * @param parent
	 */
	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL,
				true);
		createButton(parent, IDialogConstants.CANCEL_ID,
				IDialogConstants.CANCEL_LABEL, false);
		updateOkBtnState();
	}

	/**
	 * Return the initial size of the dialog.
	 */
	@Override
	protected Point getInitialSize() {
		return new Point(450, 200);
	}

	@Override
	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText(Messages.getString("GoodDialog.title")); //$NON-NLS-1$
		newShell.setImage(getParentShell().getImage());
	}

	private void updateOkBtnState() {
		Button okBtn = getButton(IDialogConstants.OK_ID);
		if (okBtn != null) {
			okBtn.setEnabled(nameTxt.getText().length() > 0
					&& normaticleTxt.getText().length() > 0
					&& supplierCv.getSelection() != null
					&& !supplierCv.getSelection().isEmpty());
		}
	}

}
